import BaseModel from '../BaseModel.js';
import brightness from '@ohos.brightness';
import storage from '@ohos.data.storage';
import LogUtil from '../../common/baseUtil/LogUtil.js';

const PREFERENCES_PATH = '/data/accounts/account_0/appdata/com.ohos.settings/sharedPreference/SettingPreferences';
var brightnessPreferences = storage.getStorageSync(PREFERENCES_PATH);
let logUtil = new LogUtil();
const BRIGHTNESS_SAVE_VALUE = 'BrightnessSaveValue';
/**
 * brightness service class
 */
export default class BrightnessSettingModel extends BaseModel {
    setBrightnessListener(brightnessValue) {
        logUtil.log('setting setBrightnessListener BrightnessSettingModel start');
        brightness.setValue(brightnessValue);
        logUtil.log('setting setBrightnessListener BrightnessSettingModel end');
    }

    setStorageValue(brightnessValue) {
        logUtil.log('setting setBrightnessListener setStorageListener start brightnessValue:' + brightnessValue);
        try {
            brightnessPreferences.putSync(BRIGHTNESS_SAVE_VALUE, brightnessValue);
            brightnessPreferences.flushSync();
        }catch (e) {
        }
        logUtil.log('setting setBrightnessListener setStorageListener end');
    }

    getStorageValue() {
        logUtil.log('setting setBrightnessListener getStorageValue start');
        try {
            return brightnessPreferences.getSync(BRIGHTNESS_SAVE_VALUE, 0);
        } catch (e){
        }
        return 50;
    }
}